<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>替换页面元素</title>
    <script>
        function replaceNode() {
            var odiv = document.getElementById('odiv')
            var p2 = document.getElementById('p2')
            var odiv2 = document.createElement('div')
            var massage = document.createTextNode('JavaScript课程')
            odiv2.appendChild(massage)
            odiv.replaceChild(odiv2, p2)


        }
    </script>
</head>

<body>
    <div id="odiv">
        <p>我是第一行内容</p>
        <p id="p2">我是第二行内容</p>
        <p>我是第三行内容</p>
        <p>我是第四行内容</p>
        <input type="button" value="将第2行内容替换为JavaScript课程" onclick="replaceNode()">
    </div>
</body>

</html>